package Dao;

import Bean.User;
import Utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-02-26
 * Time: 16:13
 */
public class UserDao {
    private Connection connection = null;
    private PreparedStatement statement = null;
    private ResultSet resultSet = null;


    //1.新增用户
    public boolean insert(User user) {
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "insert into user values(?,?,?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, user.getUserId());
            statement.setString(2, user.getUimage());
            statement.setString(3, user.getName());
            statement.setString(4, user.getPhone());
            statement.setString(5, user.getPassword());
            statement.setString(6, user.getIdentityCard());
            //3.执行sql
            int ret = statement.executeUpdate();
            if(ret == 1) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
        return false;
    }

    //2.用户注销
    public boolean delete(User user) {
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.拼装sql语句
            String sql = "delete from user where user_id = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,user.getUserId());
            //3.执行sql
            int ret = statement.executeUpdate();
            if(ret == 1) {
                System.out.println("[UserDao] 注销成功!!");
                return true;
            } else {
                System.out.println("[UserDao] 注销失败!!");
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
        return false;
    }

    //4.用户数据修改
    public boolean update(User user) {
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            String sql="UPDATE user SET name=?,phone=?,password=?,identity_card=?,uimage = ? WHERE user_id=?";
            statement=connection.prepareStatement(sql);
            statement.setString(1,user.getName());
            statement.setString(2,user.getPhone());
            statement.setString(3,user.getPassword());
            statement.setString(4,user.getIdentityCard());
            statement.setString(5, user.getUimage());
            statement.setString(6,user.getUserId());
            int ret = statement.executeUpdate();
            if(ret == 1) {

                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
        return false;
    }

    //4.获取所有用户信息 --- 分页查询
    public List<User> select (int pageNo,int pageSize) {
        List<User> array = null;
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "select * from user LIMIT ?,?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,(pageNo-1)*pageSize);
            statement.setInt(2,pageSize);
            resultSet = statement.executeQuery();
            if(resultSet != null) {
                array = new ArrayList<>();
                while(resultSet.next()) {
                    User user = new User();
                    user.setUserId(resultSet.getString("user_id"));
                    user.setName(resultSet.getString("name"));
                    user.setPhone(resultSet.getString("phone"));
                    user.setUimage(resultSet.getString("uimage"));
                    user.setPassword(resultSet.getString("password"));
                    user.setIdentityCard(resultSet.getString("identity_card"));
                    if(user.getUimage() == null || user.getUimage().isEmpty()) {
                        user.setUimage("/images/default-avatar.png");
                    }
                    array.add(user);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return array;
    }

    //查询指定id用户 --- 后续查找
    public User SelectById(String userId) {
        User user = null;
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "select * from user where user_id = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,userId);
            resultSet = statement.executeQuery();
            if(resultSet.next()) {
                user = new User();
                user.setUserId(resultSet.getString("user_id"));
                user.setName(resultSet.getString("name"));
                user.setPhone(resultSet.getString("phone"));
                user.setUimage(resultSet.getString("uimage"));
                user.setPassword(resultSet.getString("password"));
                user.setIdentityCard(resultSet.getString("identity_card"));
                if(user.getUimage() == null || user.getUimage().isEmpty()) {
                    user.setUimage("/images/default-avatar.png");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return user;
    }

    public User selectByPhone(String phone) {
        User user = null;
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "select * from user where phone = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,phone);
            resultSet = statement.executeQuery();
            if(resultSet.next()) {
                user = new User();
                user.setUserId(resultSet.getString("user_id"));
                user.setName(resultSet.getString("name"));
                user.setPhone(resultSet.getString("phone"));
                user.setUimage(resultSet.getString("uimage"));
                user.setPassword(resultSet.getString("password"));
                user.setIdentityCard(resultSet.getString("identity_card"));
                if(user.getUimage() == null || user.getUimage().isEmpty()) {
                    user.setUimage("/images/default-avatar.png");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return user;
    }

    //查询数据条数
    public int UserSize () {
        int size = 0;
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "SELECT COUNT(*) AS user_count FROM user";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            if(resultSet.next()) {
                size = resultSet.getInt("user_count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return size;
    }

    public int UserByNameSize(String UserName) {
        int size = 0;
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "SELECT COUNT(*) AS user_count FROM user where name=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,UserName);
            resultSet = statement.executeQuery();
            if(resultSet.next()) {
                size = resultSet.getInt("user_count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return size;
    }

    public List selectByName(int pageNo, int pageSize, String userName) {
        List<User> array = null;
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "select * from user  where name=? LIMIT ?,?";
            statement = connection.prepareStatement(sql);
            statement.setInt(2,(pageNo-1)*pageSize);
            statement.setInt(3,pageSize);
            statement.setString(1,userName);
            resultSet = statement.executeQuery();
            if(resultSet != null) {
                array = new ArrayList<>();
                while(resultSet.next()) {
                    User user = new User();
                    user.setUserId(resultSet.getString("user_id"));
                    user.setName(resultSet.getString("name"));
                    user.setPhone(resultSet.getString("phone"));
                    user.setUimage(resultSet.getString("uimage"));
                    user.setPassword(resultSet.getString("password"));
                    user.setIdentityCard(resultSet.getString("identity_card"));
                    if(user.getUimage() == null || user.getUimage().isEmpty()) {
                        user.setUimage("/images/default-avatar.png");
                    }
                    array.add(user);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return array;
    }

    public User selectById(String userId) {
        User user = null;
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "select * from user where user_id = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,userId);
            resultSet = statement.executeQuery();
            if(resultSet.next()) {
                user = new User();
                user.setUserId(resultSet.getString("user_id"));
                user.setName(resultSet.getString("name"));
                user.setPhone(resultSet.getString("phone"));
                user.setUimage(resultSet.getString("uimage"));
                user.setPassword(resultSet.getString("password"));
                user.setIdentityCard(resultSet.getString("identity_card"));
                if(user.getUimage() == null || user.getUimage().isEmpty()) {
                    user.setUimage("/images/default-avatar.png");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return user;
    }
}
